package com.ahyesyesteam.aicarfaultrepair.service.impl;

import com.ahyesyesteam.aicarfaultrepair.pojo.User;
import com.ahyesyesteam.aicarfaultrepair.service.IUserService;
import com.ahyesyesteam.aicarfaultrepair.service.LoginService;
import com.ahyesyesteam.aicarfaultrepair.util.JWTUtils;
import com.ahyesyesteam.aicarfaultrepair.util.Response;
import com.ahyesyesteam.aicarfaultrepair.util.SysUserUtils;
import com.ahyesyesteam.aicarfaultrepair.vo.ErrorCode;
import com.ahyesyesteam.aicarfaultrepair.vo.params.LoginParam;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    private static final int duration = 1; //token在redis中的保存时间, 单位为day

    @Autowired
    private IUserService userService ;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    String salt = "lemon**";

    @Override
    public Response login(LoginParam loginParam) {

        /**
         * 1. 检验参数合法性
         * 2. 检验密码与数据库中的是否对应
         * 3. 如果校验正确, 则生成token, 并将token存入redis
         */

        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        if(!StringUtils.hasText(account)||!StringUtils.hasText(password)){
            return Response.error(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }

        password = DigestUtils.md5Hex(password+salt);
        User user = userService.isPassWordCorrect(account, password);

        if(user == null){
            return Response.error(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }

        String token = JWTUtils.createToken(user);
        redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(user), duration, TimeUnit.DAYS);

        return Response.ok().put("token", token);
    }

    @Override
    public Response logout(String token) {
        redisTemplate.delete("TOKEN_"+token);
        return Response.ok();
    }

    @Override
    public Response register(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        String nickname = loginParam.getNickname();

        if(!SysUserUtils.isAccountValid(account)
                ||!SysUserUtils.isPasswordValid(password)
                ||!SysUserUtils.isNickName(nickname)){
            return Response.error(ErrorCode.FORMAT_ERROR.getCode(), ErrorCode.FORMAT_ERROR.getMsg());
        }

        User user = userService.findUserByAccount(account);
        if(user!=null){
            return Response.error(ErrorCode.ACCOUNT_EXIST.getCode(), ErrorCode.ACCOUNT_EXIST.getMsg());
        }

        user = userService.generateInitialUser();
        user.setAccount(account);
        user.setNickname(nickname);
        user.setPassword(DigestUtils.md5Hex(password+salt));

        userService.save(user);

        String token = JWTUtils.createToken(user);
        redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(user), 1, TimeUnit.DAYS);
        return Response.ok().put("token", token);
    }
}
